1
|
|
|
console.log('Yii2 files model init.'); |
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
var currentCroppingImageId; |
5
|
|
|
var currentRenamingFileId; |
6
|
|
|
var cropper; |
7
|
|
|
var removeFileOnCropCancel; |
8
|
|
|
var yii2CropperRoute; |
9
|
|
|
var yii2UploadRoute; |
10
|
|
|
var uploaderSettings = {}; |
11
|
|
|
|
12
|
|
|
$(document).on('change', '.yii2-files-upload-field', function () { |
13
|
|
|
|
14
|
|
|
obj = $(this); |
|
|
|
|
15
|
|
|
|
16
|
|
|
var formData = new FormData(); |
17
|
|
|
formData.append('file', obj[0].files[0]); |
18
|
|
|
formData.append('modelClass', obj.data('modelclass')); |
19
|
|
|
formData.append('attribute', obj.data('attribute')); |
20
|
|
|
formData.append('mode', obj.data('mode')); |
21
|
|
|
formData.append('ratio', obj.data('ratio')); |
22
|
|
|
formData.append('name', obj.data('name')); |
23
|
|
|
formData.append('count', 1); |
24
|
|
|
formData.append('_fileFormToken', yii2FileFormToken); |
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
$.ajax({ |
28
|
|
|
url: yii2UploadRoute, |
|
|
|
|
29
|
|
|
type: 'POST', |
30
|
|
|
data: formData, |
31
|
|
|
processData: false, // tell jQuery not to process the data |
32
|
|
|
contentType: false, // tell jQuery not to set contentType |
33
|
|
|
success: function (response) { |
34
|
|
|
id = '#files-widget-block_' + obj.data('block'); |
|
|
|
|
35
|
|
|
$(response).appendTo(id).find('div.floor12-files-widget-list'); |
36
|
|
|
} |
37
|
|
|
}); |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
function clipboard(text) { |
42
|
|
|
//based on https://stackoverflow.com/a/12693636 |
43
|
|
|
document.oncopy = function (event) { |
44
|
|
|
event.clipboardData.setData("Text", text); |
45
|
|
|
event.preventDefault(); |
46
|
|
|
}; |
47
|
|
|
document.execCommand("Copy"); |
48
|
|
|
document.oncopy = undefined; |
49
|
|
|
f12notification.info(text, 1); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function updateProgressCircle(val, btnGroup) { |
53
|
|
|
result = 169.646 * (1 - val / 100); |
|
|
|
|
54
|
|
|
btnGroup.querySelector('svg #progress-circle').setAttribute('stroke-dashoffset', result); |
55
|
|
|
btnGroup.querySelector('.floor12-file-percents').innerHTML = val + '%'; |
56
|
|
|
// setAttribute('stroke-dashoffset', result); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
var observer = new MutationObserver(function (mutations) { |
|
|
|
|
60
|
|
|
percent = mutations[0].target.style.width.replace('%', ''); |
|
|
|
|
61
|
|
|
btnGroup = mutations[0].target.parentElement.parentElement; |
|
|
|
|
62
|
|
|
updateProgressCircle(percent, btnGroup); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
var lastUploader = null; |
66
|
|
|
|
67
|
|
|
function Yii2FilesUploaderSet(id, className, attribute, scenario, name) { |
68
|
|
|
|
69
|
|
|
var mode = 'multi'; |
70
|
|
|
var blockName = "#" + id; |
71
|
|
|
var block = $(blockName); |
72
|
|
|
var uploadButton = block.find('button.btn-upload')[0]; |
73
|
|
|
var filesList = block.find('.floor12-files-widget-list')[0]; |
74
|
|
|
var ratio = 0; |
75
|
|
|
|
76
|
|
|
var csrf = block.parents('form').find('input[name=' + yii2CsrfParam + ']').val(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
if (block.data('ratio')) |
79
|
|
|
ratio = block.data('ratio'); |
|
|
|
|
80
|
|
|
|
81
|
|
|
if (block.hasClass('floor12-files-widget-single-block')) { |
82
|
|
|
mode = 'single'; |
83
|
|
|
toggleSingleUploadButton(block); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
uploaderSettings[id] = { |
87
|
|
|
modelClass: className, |
88
|
|
|
attribute: attribute, |
89
|
|
|
scenario: scenario, |
90
|
|
|
mode: mode, |
91
|
|
|
name: name, |
92
|
|
|
ratio: ratio, |
93
|
|
|
count: block.find('.floor12-files-widget-list .floor12-file-object').length, |
94
|
|
|
_fileFormToken: yii2FileFormToken |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
uploaderSettings[id][yii2CsrfParam] = csrf |
98
|
|
|
console.log(uploaderSettings[id]); |
|
|
|
|
99
|
|
|
var uploader = new ss.SimpleUpload({ |
|
|
|
|
100
|
|
|
button: uploadButton, |
101
|
|
|
url: yii2UploadRoute, |
|
|
|
|
102
|
|
|
name: 'file', |
103
|
|
|
dropzone: block, |
104
|
|
|
dragClass: 'floor12-files-widget-block-drug-over', |
105
|
|
|
multiple: true, |
106
|
|
|
multipleSelect: true, |
107
|
|
|
data: uploaderSettings[id], |
108
|
|
|
onSubmit: function (filename, extension, uploadBtn, size) { |
|
|
|
|
109
|
|
|
uploaderSettings[id].count++; |
110
|
|
|
var svg = '\t<svg class="progress-circle" width="60" height="60" viewBox="0 0 60 60">\n' + |
111
|
|
|
'\t\t<circle cx="30" cy="30" r="27" fill="none" stroke="#ccc" stroke-width="5" />\n' + |
112
|
|
|
'\t\t<circle id="progress-circle" cx="30" cy="30" r="27" fill="none" stroke="#666" stroke-width="5" stroke-dasharray="169.646" stroke-dashoffset="169.646" />\n' + |
113
|
|
|
'\t</svg>'; |
114
|
|
|
|
115
|
|
|
var fileId = generateId(filename); |
116
|
|
|
var btnGroup = document.createElement('div'); |
117
|
|
|
var fileObject = document.createElement('div'); |
118
|
|
|
var bar = document.createElement('div'); |
119
|
|
|
var percents = document.createElement('div'); |
120
|
|
|
btnGroup.setAttribute('id', fileId); |
121
|
|
|
btnGroup.className = 'btn-group files-btn-group'; |
122
|
|
|
fileObject.className = 'floor12-file-object'; |
123
|
|
|
percents.className = 'floor12-file-percents'; |
124
|
|
|
this.setProgressBar(bar); |
125
|
|
|
|
126
|
|
|
fileObject.innerHTML = svg; |
127
|
|
|
|
128
|
|
|
observer.observe(bar, { |
129
|
|
|
attributes: true |
130
|
|
|
}); |
131
|
|
|
|
132
|
|
|
fileObject.appendChild(bar); |
133
|
|
|
fileObject.appendChild(percents); |
134
|
|
|
btnGroup.appendChild(fileObject); |
135
|
|
|
|
136
|
|
|
if (mode == 'single') { |
137
|
|
|
$(filesList).html(''); |
138
|
|
|
} |
139
|
|
|
$(filesList).append(btnGroup); |
140
|
|
|
}, |
141
|
|
|
onComplete: function (filename, response) { |
142
|
|
|
if (!response) { |
143
|
|
|
console.log(filename + 'upload failed'); |
|
|
|
|
144
|
|
|
uploaderSettings[id].count--; |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
f12notification.info(FileUploadedText, 1); |
|
|
|
|
148
|
|
|
idName = "#" + generateId(filename); |
|
|
|
|
149
|
|
|
$(idName).replaceWith($(response)); |
150
|
|
|
if (mode == 'single') |
|
|
|
|
151
|
|
|
toggleSingleUploadButton(block); |
|
|
|
|
152
|
|
|
}, |
153
|
|
|
onError: function (filename, errorType, status, statusText, response, uploadBtn, fileSize) { |
|
|
|
|
154
|
|
|
console.log(uploaderSettings[id]); |
|
|
|
|
155
|
|
|
uploaderSettings[id].count--; |
156
|
|
|
data = { |
|
|
|
|
157
|
|
|
responseText: response, |
158
|
|
|
status: status, |
159
|
|
|
statusText: statusText, |
160
|
|
|
}; |
161
|
|
|
processError(data); |
162
|
|
|
idName = "#" + generateId(filename); |
|
|
|
|
163
|
|
|
$(idName).remove(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// progressUrl: 'uploadProgress.php', // enables cross-browser progress support (more info below) |
167
|
|
|
// responseType: 'json', |
168
|
|
|
// allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], |
169
|
|
|
// maxSize: 1024, // kilobytes |
170
|
|
|
// hoverClass: 'ui-state-hover', |
171
|
|
|
// focusClass: 'ui-state-focus', |
172
|
|
|
// disabledClass: 'ui-state-disabled', |
173
|
|
|
}); |
174
|
|
|
lastUploader = uploader; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
function generateId(filename) { |
178
|
|
|
return 'id-' + filename.replace(/[^0-9a-zA-Z]/g, ""); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
function showUploadButton(event) { |
182
|
|
|
obj = $(event.target); |
|
|
|
|
183
|
|
|
obj.parents('div.floor12-files-widget-single-block').find('button').show(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
function toggleSingleUploadButton(block) { |
187
|
|
|
if (block.find('div.floor12-single-file-object').length > 0) |
188
|
|
|
block.find('button').hide(); |
|
|
|
|
189
|
|
|
else |
190
|
|
|
block.find('button').show(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
function sortableFiles() { |
194
|
|
|
$(".floor12-files-widget-list-multi").sortable({ |
195
|
|
|
opacity: 0.5, |
196
|
|
|
revert: 1, |
197
|
|
|
items: "div.files-btn-group", |
198
|
|
|
connectWith: ".floor12-files-widget-list-multi" |
199
|
|
|
}); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
function removeFile(id) { |
203
|
|
|
id = "#yii2-file-object-" + id; |
204
|
|
|
const blockId = $(id).parents('.files-widget-block').attr('id'); |
205
|
|
|
|
206
|
|
|
$(id).parents('div.files-btn-group').fadeOut(200, function () { |
207
|
|
|
$(this).remove(); |
208
|
|
|
f12notification.info(FileRemovedText, 1); |
|
|
|
|
209
|
|
|
}); |
210
|
|
|
|
211
|
|
|
uploaderSettings[blockId].count--; |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
function removeAllFiles(event) { |
216
|
|
|
console.log('removeAllFiles'); |
|
|
|
|
217
|
|
|
$(event.target).parents('div.floor12-files-widget-list').find('div.files-btn-group').fadeOut(200, function () { |
218
|
|
|
$(this).remove(); |
219
|
|
|
}); |
220
|
|
|
const blockId = $(event.target).parents('.floor12-files-widget-block').attr('id'); |
221
|
|
|
uploaderSettings[blockId].count = 0; |
222
|
|
|
f12notification.info(FilesRemovedText, 1); |
|
|
|
|
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
function initCropperLayout() { |
227
|
|
|
if (yii2CropperRoute.length > 0) |
|
|
|
|
228
|
|
|
$.get(yii2CropperRoute, function (response) { |
|
|
|
|
229
|
|
|
$('body').append(response); |
230
|
|
|
|
231
|
|
|
$('#yii2-file-title-editor input').on('keyup', function (e) { |
232
|
|
|
if (e.keyCode == 13) { |
233
|
|
|
saveFileTitle() |
234
|
|
|
} |
235
|
|
|
}); |
236
|
|
|
}) |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
function initCropper(id, url, ratio, remove) { |
240
|
|
|
$('#cropperModal').modal({keyboard: false, backdrop: 'static'}); |
241
|
|
|
|
242
|
|
|
currentCroppingImageId = id; |
243
|
|
|
|
244
|
|
|
removeFileOnCropCancel = false; |
245
|
|
|
if (remove) |
246
|
|
|
removeFileOnCropCancel = true; |
|
|
|
|
247
|
|
|
|
248
|
|
|
currentCropImage = $('<img>').attr('src', url); |
|
|
|
|
249
|
|
|
$('#cropperArea').html(""); |
250
|
|
|
$('#cropperArea').append(currentCropImage); |
251
|
|
|
|
252
|
|
|
autoCrop = false; |
|
|
|
|
253
|
|
|
aspectRatio = NaN; |
|
|
|
|
254
|
|
|
$('#cropper-btn-cancel').show(); |
255
|
|
|
|
256
|
|
|
console.log(cropperHideCancel); |
|
|
|
|
257
|
|
|
if (cropperHideCancel == 'true') { |
258
|
|
|
$('#cropper-btn-cancel').hide(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
if (ratio) { |
263
|
|
|
autoCrop = true; |
264
|
|
|
aspectRatio = ratio; |
265
|
|
|
$('.cropper-ratio-btn-group').hide(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
setTimeout(function () { |
269
|
|
|
cropper = currentCropImage.cropper({ |
270
|
|
|
viewMode: 1, |
271
|
|
|
background: false, |
272
|
|
|
zoomable: false, |
273
|
|
|
autoCrop: autoCrop, |
274
|
|
|
aspectRatio: aspectRatio, |
275
|
|
|
}); |
276
|
|
|
}, 1000) |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
function stopCrop(id) { |
|
|
|
|
280
|
|
|
$('#cropperModal').modal('hide'); |
281
|
|
|
if (removeFileOnCropCancel) |
282
|
|
|
removeFile(currentCroppingImageId); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
function cropImage() { |
286
|
|
|
сropBoxData = cropper.cropper('getCropBoxData'); |
|
|
|
|
287
|
|
|
imageData = cropper.cropper('getImageData'); |
|
|
|
|
288
|
|
|
canvasData = cropper.cropper('getCanvasData'); |
|
|
|
|
289
|
|
|
ratio = imageData.height / imageData.naturalHeight; |
|
|
|
|
290
|
|
|
cropLeft = (сropBoxData.left - canvasData.left) / ratio; |
|
|
|
|
291
|
|
|
cropTop = (сropBoxData.top - canvasData.top) / ratio; |
|
|
|
|
292
|
|
|
cropWidth = сropBoxData.width / ratio; |
|
|
|
|
293
|
|
|
cropHeight = сropBoxData.height / ratio; |
|
|
|
|
294
|
|
|
rotated = imageData.rotate; |
|
|
|
|
295
|
|
|
|
296
|
|
|
data = { |
|
|
|
|
297
|
|
|
id: currentCroppingImageId, |
298
|
|
|
width: cropWidth, |
299
|
|
|
height: cropHeight, |
300
|
|
|
top: cropTop, |
301
|
|
|
left: cropLeft, |
302
|
|
|
rotated: rotated, |
303
|
|
|
_fileFormToken: yii2FileFormToken |
|
|
|
|
304
|
|
|
}; |
305
|
|
|
|
306
|
|
|
removeFileOnCropCancel = false; |
307
|
|
|
|
308
|
|
|
$.ajax({ |
309
|
|
|
url: yii2CropRoute, |
|
|
|
|
310
|
|
|
'method': 'POST', |
311
|
|
|
data: data, |
312
|
|
|
success: function (response) { |
313
|
|
|
id = '#yii2-file-object-' + currentCroppingImageId; |
|
|
|
|
314
|
|
|
if ($(id).find('img').length) |
315
|
|
|
$(id).find('img').attr('src', response); |
|
|
|
|
316
|
|
|
else { |
317
|
|
|
$(id).css('background-image', 'none'); |
318
|
|
|
$(id).css('background-image', 'url(' + response + ')'); |
319
|
|
|
} |
320
|
|
|
stopCrop(); |
321
|
|
|
f12notification.info(FileSavedText, 1); |
|
|
|
|
322
|
|
|
}, |
323
|
|
|
error: function (response) { |
324
|
|
|
processError(response); |
325
|
|
|
} |
326
|
|
|
}) |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
function showRenameFileForm(id, event) { |
330
|
|
|
var blockId = '#yii2-file-object-' + id; |
331
|
|
|
var title = $(blockId).attr('title'); |
332
|
|
|
currentRenamingFileId = id; |
333
|
|
|
$('#yii2-file-title-editor').css('top', event.clientY).css('left', event.clientX - 70).fadeIn(100); |
334
|
|
|
$('#yii2-file-title-editor input').val(title).focus(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
function showAltForm(id, event) { |
338
|
|
|
var blockId = '#yii2-file-object-' + id; |
339
|
|
|
var alt = $(blockId).data('alt'); |
340
|
|
|
console.log(alt); |
|
|
|
|
341
|
|
|
currentRenamingFileId = id; |
342
|
|
|
$('#yii2-file-alt-editor').css('top', event.clientY).css('left', event.clientX - 70).fadeIn(100); |
343
|
|
|
$('#yii2-file-alt-editor input').val(alt).focus(); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
function hideYii2FileTitleEditor() { |
347
|
|
|
$('#yii2-file-title-editor').fadeOut(100); |
348
|
|
|
currentRenamingFileId = null; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
function hideYii2FileAltEditor() { |
352
|
|
|
$('#yii2-file-alt-editor').fadeOut(100); |
353
|
|
|
currentRenamingFileId = null; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
function saveFileTitle() { |
357
|
|
|
$('#yii2-file-title-editor').fadeOut(100); |
358
|
|
|
val = $('#yii2-file-title-editor input').val(); |
|
|
|
|
359
|
|
|
blockId = '#yii2-file-object-' + currentRenamingFileId; |
|
|
|
|
360
|
|
|
$(blockId).attr('title', val); |
361
|
|
|
$(blockId).attr('data-title', val); |
362
|
|
|
|
363
|
|
|
$.ajax({ |
364
|
|
|
url: yii2RenameRoute, |
|
|
|
|
365
|
|
|
method: 'POST', |
366
|
|
|
data: {id: currentRenamingFileId, title: val, _fileFormToken: yii2FileFormToken}, |
|
|
|
|
367
|
|
|
success: function () { |
368
|
|
|
f12notification.info(FileRenamedText, 1); |
|
|
|
|
369
|
|
|
}, |
370
|
|
|
error: function (response) { |
371
|
|
|
processError(response); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
); |
375
|
|
|
currentRenamingFileId = null; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
function saveFileAlt() { |
379
|
|
|
$('#yii2-file-alt-editor').fadeOut(100); |
380
|
|
|
val = $('#yii2-file-alt-editor input').val(); |
|
|
|
|
381
|
|
|
blockId = '#yii2-file-object-' + currentRenamingFileId; |
|
|
|
|
382
|
|
|
$(blockId).data('alt', val) |
383
|
|
|
|
384
|
|
|
$.ajax({ |
385
|
|
|
url: yii2AltRoute, |
|
|
|
|
386
|
|
|
method: 'POST', |
387
|
|
|
data: {id: currentRenamingFileId, alt: val, _fileFormToken: yii2FileFormToken}, |
|
|
|
|
388
|
|
|
success: function () { |
389
|
|
|
f12notification.info(FileRenamedText, 1); |
|
|
|
|
390
|
|
|
}, |
391
|
|
|
error: function (response) { |
392
|
|
|
processError(response); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
); |
396
|
|
|
currentRenamingFileId = null; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$(document).ready(function () { |
400
|
|
|
setInterval(function () { |
401
|
|
|
sortableFiles() |
402
|
|
|
}, 2000); |
403
|
|
|
|
404
|
|
|
sortableFiles(); |
405
|
|
|
|
406
|
|
|
initCropperLayout(); |
407
|
|
|
|
408
|
|
|
}); |
409
|
|
|
|
410
|
|
|
|
411
|
|
|
|